package com.gccloud.design.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.ProcessCustomDao;
import com.gccloud.design.dto.BwpBusinessProcessDto;
import com.gccloud.design.entity.BwpBusinessProcessEntity;
import com.gccloud.design.entity.StfBusinessProcessEntity;
import com.gccloud.design.feign.BwpWebClient;
import com.gccloud.design.service.ProcessCustomService;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 流程定制
 *
 * @author guo.wang
 */
@Service
public class ProcessCustomServiceImpl extends ServiceImpl<ProcessCustomDao, BwpBusinessProcessEntity> implements ProcessCustomService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProcessCustomServiceImpl.class);

    @Resource
    private BwpWebClient bwpWebClient;


    @Resource
    private ProcessCustomDao processCustomDto;

    /**
     * 新增/修改流程
     *
     * @param bwpBusinessProcessDto 流程dto
     * @return 新增/修改数量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateProcess(BwpBusinessProcessDto bwpBusinessProcessDto) {
        BwpBusinessProcessEntity bwpBusinessProcessEntity = BeanConvertUtils.convert(bwpBusinessProcessDto, BwpBusinessProcessEntity.class);
        Integer id = bwpBusinessProcessEntity.getId();
        boolean result = false;
        try {
            if (id == null) {
                bwpBusinessProcessEntity.setDelFlag(0).setFromId(0).setCreateUser("admin").setUpdateUser("admin").setShardingId(0);
                String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                bwpBusinessProcessEntity.setCreateTime(format);
                bwpBusinessProcessEntity.setUpdateTime(format);
                bwpBusinessProcessEntity.setBwpFlowId("0");
                // db
                result = this.save(bwpBusinessProcessEntity);
                // 调用流程平台新增
                StfBusinessProcessEntity stfBusinessProcessEntity = new StfBusinessProcessEntity();
                stfBusinessProcessEntity.setBusinessTypeId(bwpBusinessProcessEntity.getBusinessTypeId());
                stfBusinessProcessEntity.setFlowName(bwpBusinessProcessEntity.getFlowName());
                stfBusinessProcessEntity.setType(bwpBusinessProcessEntity.getType());
                stfBusinessProcessEntity.setMemo(bwpBusinessProcessEntity.getMemo());
                String processId = bwpWebClient.insertProcess(stfBusinessProcessEntity).getData();
                bwpBusinessProcessEntity.setBwpFlowId(processId);
            } else {
                // db
                result = this.updateById(bwpBusinessProcessEntity);
                // 调用流程平台新增
                StfBusinessProcessEntity stfBusinessProcessEntity = new StfBusinessProcessEntity();
//                stfBusinessProcessEntity.setId(bwpBusinessProcessEntity.getBwpFlowId());
                stfBusinessProcessEntity.setFlowName(bwpBusinessProcessEntity.getFlowName());
                stfBusinessProcessEntity.setType(bwpBusinessProcessEntity.getType());
                stfBusinessProcessEntity.setMemo(bwpBusinessProcessEntity.getMemo());
                bwpWebClient.updateProcess(stfBusinessProcessEntity);
            }
            LOGGER.info("新增/修改{db}：：bwpBusinessProcessEntity：" + bwpBusinessProcessEntity);
        } catch (Exception e) {
            LOGGER.error("异常信息{}", e.getMessage());
        }
        return result ? 1 : 0;
    }

    /**
     * 删除流程
     *
     * @param bpId 流程id
     * @return 删除数量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProcess(Integer bpId) {
        boolean result = false;
        try {
            if (bpId == null) {
                throw new GlobalException(("没有该流程"));
            }
            // 设置delFlag为1
            BwpBusinessProcessEntity bwpBusinessProcessEntityById = this.getById(bpId);
            bwpBusinessProcessEntityById.setDelFlag(1);
            // db
            result = this.updateById(bwpBusinessProcessEntityById);
            // 调用流程平台接口删除
            bwpWebClient.deleteProcess(bwpBusinessProcessEntityById.getBwpFlowId());
            LOGGER.info("删除{db}：：BwpBusinessProcessEntity：" + bwpBusinessProcessEntityById);
        } catch (Exception e) {
            LOGGER.error("异常信息{}", e.getMessage());
        }
        return result ? 1 : 0;
    }

    /**
     * 流程定制树db
     *
     * @param id 父节点id
     * @return List<Map < String, Object>>
     */
    @Override
    public List<Map<String, Object>> getProcessTree(Integer id) {
        return processCustomDto.getProcessTree(id);
    }
}
